package com.dc.project.util;

import com.own.component.common.util.encrypt.EncryptUtil;
import com.own.exception.BusinessSimpleException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * TaskPasswordUtil
 *
 * @author chenxueli
 * @date 2025-04-22 10:31:00
 */
@Slf4j
@Component
public class TaskPasswordUtil {

    @Resource
    private HttpServletRequest request;

    /**
     * 检查密码
     *
     * @param taskPassword 任务密码
     */
    public void check(String taskPassword) {
        // 获取请求头中用户传入的密码
        var inputPassword = request.getHeader("input-password");
        check(taskPassword, inputPassword);
    }

    /**
     * 检查密码
     *
     * @param taskPassword  任务密码
     * @param inputPassword 用户输入的密码
     */
    public void check(String taskPassword, String inputPassword) {
        if (StringUtils.isBlank(taskPassword)) {
            return;
        }
        // 对数据进行加密处理
        var encryptInputPassword = EncryptUtil.MD5.encrypt(inputPassword);
        // 进行数据比较
        if (!encryptInputPassword.equalsIgnoreCase(taskPassword)) {
            throw new BusinessSimpleException("C0001", "密码错误");
        }
    }

}
